package fr.epsi.spring1.dao.tests;

import java.util.List;

import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import fr.epsi.spring1.dao.ArticleDao;
import fr.epsi.spring1.domain.Article;

public class TestArticleDao {

	private final static Logger logger = Logger.getLogger(TestArticleDao.class);

	private static ArticleDao articleDao;

	@BeforeClass
	public static void init() {
		logger.error("Init test UserDao");
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("testapplicationContext.xml");
		articleDao = context.getBean(ArticleDao.class);
	}

	@Test
	public void testSelect() {
		logger.error("Select test");
		List<Article> listOfArticles = articleDao.select("1");
		if (listOfArticles == null) {
			Assert.fail();
		} else {
			for (Article article : listOfArticles) {
				logger.info("User=" + article.toString());
			}
		}
	}

	@Test
	public void testGetArticle() {
		logger.error("Select artivle ref 1");
		Article article = articleDao.getArticle("1");
		if (article == null) {
			Assert.fail();
		}
	}

	@Test
	public void testInsert() {
		logger.error("Insert test");
		Article article = new Article();
		article.setReference("1");
		article.setNom("Ipad");
		article.setDescription("tablette");
		article.setPrix(Float.valueOf(400));
		article.setEpuise(false);
		if (articleDao.insert(article)) {
			Assert.fail();
		}
	}

	@Test
	public void testUpdate() {
		logger.error("Update test");
		Article article = new Article();
		article.setReference("1");
		article.setNom("Ipad");
		article.setDescription("tablette Retina");
		article.setPrix(Float.valueOf(500));
		article.setEpuise(false);
		articleDao.update(article);
	}

}
